package ru.ipo.dces.plugins.admin.resultstable;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Посетитель
 * Date: 11.04.2009
 * Time: 16:26:12
 */
public class ResultsTableModel implements TableModel {

    private TableCellRenderer centeredTextRenderer = new CenteredTextTableCellRenderer();

    private String[] headers;
    private String[][] minorHeaders;
    private KeySet[] keySets;
    private String[][][] table;

    private final int cols;
    private final int rows;

    private HashMap<Integer, String> col2name = new HashMap<Integer, String>();

    private int ind1, ind2;

    private void col2inds(int col) {
        ind1 = 0;
        while (true) {

            int l;
            if (keySets[ind1] != null)
                l = 1;
            else
                l = minorHeaders[ind1].length;

            if (col < l)
                break;

            col -= l;
            ind1++;
        }
        ind2 = col;
    }

    public ResultsTableModel(String[] headers, String[][] minorHeaders, String[][][] table) {
        this.headers = headers;
        this.minorHeaders = minorHeaders;
        this.table = table;

        int minorHeadersLength = minorHeaders.length;

        keySets = new KeySet[minorHeadersLength];

        int c = 0;
        for (int i = 0; i < minorHeadersLength; i++) {
            String[] minorHeader = minorHeaders[i];
            KeySetsRegistry reg = KeySetsRegistry.getInstance();
            keySets[i] = reg.getKeySet(minorHeader);

            if (keySets[i] != null)
                c += 1;
            else
                c += minorHeader.length;
        }

        cols = c;

        rows = table.length;
    }

    public int getRowCount() {
        return rows;
    }

    public int getColumnCount() {
        return cols;
    }

    public String getColumnName(int columnIndex) {
        String res = col2name.get(columnIndex);
        if (res == null) {
            col2inds(columnIndex);

            res = "<b>" + headers[ind1] + "</b>";
            if (keySets[ind1] == null && minorHeaders[ind1].length > 1)
                res = "<center>" + res + "<br>" + minorHeaders[ind1][ind2] + "</center>";
            res = "<html>" + res + "</html>";

            col2name.put(columnIndex, res);
        }

        return res;
    }

    public Class<?> getColumnClass(int columnIndex) {
        col2inds(columnIndex);
        return keySets[ind1] == null ? String.class : String[].class;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        col2inds(columnIndex);
        String[] problemResult = table[rowIndex][ind1];

        if (keySets[ind1] == null && problemResult != null)
            return problemResult[ind2];
        else if (problemResult == null)
            return null;
        else {
            Map<String, String> result = new HashMap<String, String>();
            for (int i = 0; i < minorHeaders[ind1].length; i++)
                result.put(minorHeaders[ind1][i], problemResult[i]);
            return result;
        }
    }

    public TableCellRenderer getRenderer(int columnIndex) {
        col2inds(columnIndex);
        KeySet keySet = keySets[ind1];
        if (keySet == null)
            return centeredTextRenderer;
        else if (keySet.getRenderer() != null)
            return keySet.getRenderer();
        else
            return new MapTableCellRenderer();
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //do nothing
    }

    public void addTableModelListener(TableModelListener l) {
        //do nothing
    }

    public void removeTableModelListener(TableModelListener l) {
        //do nothing
    }

}
